package ejb.userManagement;

import ejb.dto.UserDTO;
import ejb.userManagement.AccountMgr;
import entities.Group;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import entities.User;

/**
 * Session Bean implementation class UserBean
 */
@Stateless
public class AccountBean implements AccountMgr {

	@PersistenceContext
    private EntityManager em;
	
	@Resource
	private EJBContext context;
	

	@Override
	public boolean save(UserDTO user) {
		try{
			User newUser = new User(user);
			List<Group> groups = new ArrayList<Group>();
			groups.add(Group.USERS);
			newUser.setGroups(groups);
			em.persist(newUser);
			em.flush();
		}
		catch(Exception e){
			return false;
		}
		return true;
	}
	
	public static UserDTO convertToDTO(User user){
		UserDTO dto = new UserDTO();
		dto.setEmail(user.getUserId());
		dto.setFirstName(user.getFirstName());
		dto.setLastName(user.getLastName());
		dto.setPassword(user.getPassword());
		return dto;
	}


}
